using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Threading;
namespace MicroMsg.UI.Page
{
	public class TxtAttachmentViewer : BasePage
	{
		private const string HTML_FILE = "TxtAttachmentViewer.html";
		private const string TAG = "TxtAttachmentViewer";
		private string content;
		private ChatMsg mCuMsg;
		private int imageLen;
		private EventWatcher downLoadSuccessWatcher;
		private EventWatcher downLoadFailWatcher;
		private DispatcherTimer ProgressTimer;
		private bool isGuessProgressStart;
		public double currentProgress;
		internal Grid LayoutRoot;
		internal Grid ContentPanel;
		internal WebBrowser webBrowser;
		internal Canvas LoadingView;
		internal Image DefaultImg;
		internal DownloadProgressBar DownLoadProgressBar;
		internal TextBlock InfoTxt;
		private bool _contentLoaded;
		public TxtAttachmentViewer()
		{
			this.InitializeComponent();
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 == null || object_0.Length < 2)
			{
				return;
			}
			this.mCuMsg = (object_0[0] as ChatMsg);
			int num = (int)object_0[1];
			if (this.mCuMsg == null || !this.mCuMsg.IsAppMsg())
			{
				return;
			}
			if (this.mCuMsg.strPath != null && this.mCuMsg.strPath.get_Length() > 0 && StorageIO.fileExists(this.mCuMsg.strPath))
			{
				this.ShowTextContent(this.mCuMsg.strPath);
				return;
			}
			if (num > 0)
			{
				this.imageLen = num / 1000;
			}
			else
			{
				this.imageLen = 50;
			}
			this.initWatcher();
			if (ServiceCenter.sceneDownloadAppAttach.doScene(this.mCuMsg))
			{
				if (!NetworkDeviceWatcher.isCurrentWifi())
				{
					this.GuessProgressStart();
					return;
				}
			}
			else
			{
				this.ShowFailView();
			}
		}
		private void webBrowser_loaded(object sender, EventArgs e)
		{
			Thickness margin = this.webBrowser.get_Margin();
			margin.set_Left(0.0);
			this.webBrowser.set_Margin(margin);
		}
		private void webView_Navigated(object sender, NavigationEventArgs e)
		{
		}
		protected override void OnPageOut()
		{
			this.unitWatcher();
			ServiceCenter.sceneDownloadAppAttach.cancel();
		}
		private void writeHtmlFile(string filePath)
		{
		}
		public static void GoToThisPage(ChatMsg mCuMsg, int totalLen, bool isRemoveMe = false)
		{
			ToolFunction.GotoPageForUserControl("/Source/UI/Util/TxtAttachmentViewer.xaml", new object[]
			{
				mCuMsg,
				totalLen
			}, isRemoveMe);
		}
		private void initWatcher()
		{
			this.downLoadSuccessWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.on_LoadSuccess_EventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_COMPLETE, this.downLoadSuccessWatcher);
			this.downLoadFailWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.on_LoadFail_EventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_FAIL, this.downLoadFailWatcher);
		}
		private void unitWatcher()
		{
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_COMPLETE, this.downLoadSuccessWatcher);
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_DOWNLOAD_ATTACH_FAIL, this.downLoadFailWatcher);
		}
		private void GuessProgressStart()
		{
			if (this.isGuessProgressStart)
			{
				return;
			}
			this.ProgressTimer = new DispatcherTimer();
			this.ProgressTimer.add_Tick(new EventHandler(this.ProgressTimer_Tick));
			this.ProgressTimer.set_Interval(new TimeSpan(0, 0, 0, 0, 500));
			this.isGuessProgressStart = true;
			this.ProgressTimer.Start();
		}
		private void ProgressTimer_Tick(object sender, EventArgs e)
		{
			if (this.DownLoadProgressBar.get_Value() >= 80.0)
			{
				return;
			}
			DownloadProgressBar expr_1D = this.DownLoadProgressBar;
			expr_1D.set_Value(expr_1D.get_Value() + (double)(new Random().Next(3) + 2));
			int num = (int)(this.DownLoadProgressBar.get_Value() / 100.0 * (double)this.imageLen);
			this.InfoTxt.set_Text(string.Format(strings.COM_DownloadInfo, num, this.imageLen));
		}
		private void GuessProgressEnd()
		{
			if (!this.isGuessProgressStart)
			{
				return;
			}
			this.isGuessProgressStart = false;
			this.ProgressTimer.Stop();
			this.ProgressTimer = null;
		}
		private void on_LoadSuccess_EventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			Log.smethod_5(base.GetType().get_Name(), "on_LoadSuccess_EventHandler");
			this.GuessProgressEnd();
			if (evtArgs == null || evtArgs.mObject == null)
			{
				this.ShowFailView();
				return;
			}
			ParamDownloadAppAttach paramDownloadAppAttach = evtArgs.mObject as ParamDownloadAppAttach;
			if (paramDownloadAppAttach == null)
			{
				this.ShowFailView();
				return;
			}
			string mImagePath = paramDownloadAppAttach.mImagePath;
			if (string.IsNullOrEmpty(mImagePath))
			{
				this.ShowFailView();
				return;
			}
			this.ShowTextContent(mImagePath);
		}
		private void on_LoadFail_EventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			this.GuessProgressEnd();
			Log.smethod_5(base.GetType().get_Name(), "on_LoadFail_EventHandler");
			this.ShowFailView();
		}
		public void ShowTextContent(string imgUrl)
		{
			this.webBrowser.add_LoadCompleted(new LoadCompletedEventHandler(this.webBrowser_loaded));
			this.webBrowser.set_Source(new Uri(imgUrl, 2));
			this.LoadingView.set_Visibility(1);
			this.webBrowser.set_Visibility(0);
		}
		public void ShowFailView()
		{
			this.LoadingView.set_Visibility(0);
			this.webBrowser.set_Visibility(1);
			this.InfoTxt.set_Text(strings.ImageViewDownError);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Util/TxtAttachmentViewer.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.ContentPanel = (Grid)base.FindName("ContentPanel");
			this.webBrowser = (WebBrowser)base.FindName("webBrowser");
			this.LoadingView = (Canvas)base.FindName("LoadingView");
			this.DefaultImg = (Image)base.FindName("DefaultImg");
			this.DownLoadProgressBar = (DownloadProgressBar)base.FindName("DownLoadProgressBar");
			this.InfoTxt = (TextBlock)base.FindName("InfoTxt");
		}
	}
}
